<?php

namespace app\order\controller;

use app\common\controller\PCBase;
use app\common\library\Upload;
use app\order\logic\OrderWhLogic;
use app\order\model\Orderwh as OrderWhModel;
use think\facade\Db;
use Throwable;

class OrderWh extends PCBase
{
    /**
     * 模型
     * @var object
     * @phpstan-var OrderWhModel
     */
    protected object $model;

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new OrderWhModel();
    }

    /**
     * 查看-列表
     * @throws Throwable
     */
    public function index()
    {
        $params = $this->request->post();
        $orderLogic = new OrderWhLogic();
        return $orderLogic->getOrderWhList($params);
    }

    /**
     * 编辑
     * @throws Throwable
     */
    public function edit($id = null)
    {
        $row = $this->model->find($id);
        if (!$row) {
            return json(DefaultResponse(201, false, '记录不能为空!', null));
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();

            $orderLogic = new OrderWhLogic();
            $result = $orderLogic->editOrder($row, $data);

            return $result;
        }

        return json(DefaultResponse(200, true, '获取成功', ['row' => $row]));
    }



    /**
     * 自动发货
     */
    public function automaticShipping()
    {
        $params = $this->request->post();
        $orderShipLogic = new OrderWhLogic();
        $result = $orderShipLogic->automaticShipping($params);
        return $result;
    }

    /**
     * 无发货渠道
     * @return \think\response\Json
     */
    public function markerOrder()
    {
        $params = $this->request->post();
        $orderShipLogic = new OrderWhLogic();
        $result = $orderShipLogic->markerOrder($params);
        return $result;
    }

    /**
     * 订单导出
     */
    public function export()
    {
        $params = $this->request->post();
        $orderLogic = new OrderWhLogic();
        return $orderLogic->exportOrder($params);
    }

    /**
     * 上传网红订单
     */
    public function uploadOrder()
    {
        $file       = $this->request->file('file');
        $upload     = new Upload($file);
        $attachment = $upload->upload(null, $this->getAdminId());
        $orderLogic = new OrderWhLogic();
        $return     = $orderLogic->insertOrderWh($attachment['url']);
        return  $this->jsonMessage(200, true, $return,null);
    }


    /**
     * @desc  重复发送的转为已处理
     * @date  2024-05-23
     * @author luckyXiong
     */
    public function endShipping(){
        $params         = $this->request->post();
        $res            = (new OrderWhLogic())->endShipping($params);
        return  $this->jsonMessage(200, true, '操作成功',$res);

    }





}
